From: Phil Race Date: Sat, 28 Feb 2026 22:58:10 +0000 (+0000) Subject: [PATCH] 8378607: GlyphLayout cache can prevent Fonts from being GC'd X-Git-Tag: archive/raspbian/25.0.5_7ea-1+rpi1~5 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=6cf195ff470fb12f8116b73182b799d130571c3f;p=openjdk-25.git [PATCH] 8378607: GlyphLayout cache can prevent Fonts from being GC'd From f3d7ca33d797c3475a4c31d612e7ed8f71d1f5b0 Mon Sep 17 00:00:00 2001 Bug: https://bugs.openjdk.org/browse/JDK-8378607 Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/openjdk-25/+bug/2155650 Origin: upstream, https://github.com/openjdk/jdk/commit/f3d7ca33d797c3475a4c31d612e7ed8f71d1f5b0 Reviewed-by: jdv, tr Gbp-Pq: Name jdk-8378607.diff --- diff --git a/src/java.desktop/share/classes/sun/font/GlyphLayout.java b/src/java.desktop/share/classes/sun/font/GlyphLayout.java index df42cce34..5bff127f1 100644 --- a/src/java.desktop/share/classes/sun/font/GlyphLayout.java +++ b/src/java.desktop/share/classes/sun/font/GlyphLayout.java @@ -76,7 +76,7 @@ import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.util.ArrayList; -import java.util.concurrent.ConcurrentHashMap; +import java.util.WeakHashMap; import static java.lang.Character.*; @@ -125,18 +125,12 @@ public final class GlyphLayout { } private static final class SDCache { - public Font key_font; - public FontRenderContext key_frc; - public AffineTransform dtx; public AffineTransform gtx; public Point2D.Float delta; public FontStrikeDesc sd; private SDCache(Font font, FontRenderContext frc) { - key_font = font; - key_frc = frc; - // !!! add getVectorTransform and hasVectorTransform to frc? then // we could just skip this work... @@ -177,7 +171,7 @@ public final class GlyphLayout { private static final Point2D.Float ZERO_DELTA = new Point2D.Float(); private static - SoftReference> cacheRef; + SoftReference> cacheRef; private static final class SDKey { private final Font font; @@ -232,7 +226,7 @@ public final class GlyphLayout { } SDKey key = new SDKey(font, frc); // garbage, yuck... - ConcurrentHashMap cache = null; + WeakHashMap cache = null; SDCache res = null; if (cacheRef != null) { cache = cacheRef.get(); @@ -243,13 +237,17 @@ public final class GlyphLayout { if (res == null) { res = new SDCache(font, frc); if (cache == null) { - cache = new ConcurrentHashMap(10); + cache = new WeakHashMap(10); cacheRef = new - SoftReference>(cache); - } else if (cache.size() >= 512) { - cache.clear(); + SoftReference>(cache); + } else if (cache.size() >= 128) { + synchronized (SDCache.class) { + cache.clear(); + } + } + synchronized (SDCache.class) { + cache.put(key, res); } - cache.put(key, res); } return res; }